/***************************************************************************
 *   Copyright (C) 2009 by mj   *
 *   mj_dv@web.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

// installMani.cpp is a part of the steamserverutil project it is the GUI using Qt 4 for the console app steamserverutil
// steamserverutil.googlecode.com

#include <QNetworkRequest>
#include <QFile>
#include <QIODevice>
#include <QMessageBox>
#include <QProcess>

#include "installmani.h"
#include "steamserverutil.h"


installMani::installMani(QWidget *parent) :
    QDialog(parent)
{
    setupUi(this);

    util = new steamserverutil();

    util->is_installed(comboBox);


    progressBar->setVisible(false);

    connect(installButton, SIGNAL(clicked()), this, SLOT(install()));
    connect(installButton, SIGNAL(clicked()), this, SLOT(install()));

}

void installMani::install()
{
    if(whichComboBox->currentIndex() == 0)
        installM();
    else
        installQ();
}

void installMani::installM()
{

    int ch = comboBox->currentIndex();

    if(ch > 4)
    {
        QMessageBox::critical(this, tr("Error installing!"), tr("Sorry but this server is not suported by Mani Admin Plugin!"), QMessageBox::Ok | QMessageBox::Default);
    }

    statusLabel->setText(tr("Downloading .. "));



    util->install_mani(ch); // to write vdf

    util->server_path(util->server[ch]);
    path = util->param.c_str();

    progressBar->setVisible(true);

    manager = new QNetworkAccessManager(this);

    if (ch==5)
        reply = manager->get(QNetworkRequest(QUrl("http://steamserverutil.googlecode.com/files/mani_admin_plugin_v1_2_beta_s_orange.zip")));


    else

         reply = manager->get(QNetworkRequest(QUrl("http://steamserverutil.googlecode.com/files/mani_admin_plugin_v1_2_beta_s.zip")));


    connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(set_progress(qint64, qint64)));
    connect(reply, SIGNAL(finished()), this, SLOT(save()));

    installButton->setEnabled(false);



}

void installMani::installQ()
{
    int ch = comboBox->currentIndex();

    if(ch > 4)
    {
        QMessageBox::critical(this, tr("Error installing!"), tr("Sorry but this server is not suported by Mani Admin Plugin!"), QMessageBox::Ok | QMessageBox::Default);
    }

    statusLabel->setText(tr("Downloading .. "));



    util->server_path(util->server[ch]);
    path = util->param.c_str();

    progressBar->setVisible(true);

    manager = new QNetworkAccessManager(this);
    reply = manager->get(QNetworkRequest(QUrl("http://steamserverutil.googlecode.com/files/quake_pack.zip")));


    connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(set_progress(qint64, qint64)));
    connect(reply, SIGNAL(finished()), this, SLOT(save()));


    installButton->setEnabled(false);


}

void installMani::set_progress(qint64 cur, qint64 max)
{
    progressBar->setMaximum(max);
    progressBar->setValue(cur);
}

void installMani::save()
{
    QByteArray data = reply->readAll();


    QFile out(path+"temp.zip");

    if (!out.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        QMessageBox::critical(this, tr("Error writing data"), tr("Cannot write data: "), QMessageBox::Ok | QMessageBox::Default);
        return;
    }
    out.write(data);
    out.close();

    statusLabel->setText(tr("Unpacking ... "));

    QProcess *proc = new QProcess(this);

    proc->setWorkingDirectory(path);
    proc->start("unzip -o temp.zip");

    statusLabel->setText(tr("Success!"));

    installButton->setEnabled(true);


}

